<?php
/**
 * 树管理
 * 
 * 制作:  叶风
 * Qq:    355007778
 * Email: ye.fn@163.com
 *
 */
namespace m;
class tree extends ad
{
    use tree_g;
    
    /**
     * 
     * @param string $f
     * @param array $p
     */
	function setTemplate($f, $p=array())
	{
		$this->loadTemplate($this->t_p .DIRECTORY_SEPARATOR .$f,  $p);
	}
    
    // home
    function index_home()
    {
        $this->PU->check($this->auth_m);
        $this->title = \lang::g($this->title);
		$this->setTemplate('home');
    }
    function index_list_a()
    {
        $this->PU->check($this->auth_m);
        $this->t_var['dataR'] = $this->list_a($_GET['id']);
        $this->setTemplate('list_a');
        $this->display('');
    }
    function list_a($pid)
    {
		$pid = intval($pid);
        $tables= array('t1'=>array('as'=>$this->table));
		$tables['sub'] = array('as'=>$this->table,'join'=>'LEFT JOIN','on'=>'t1.id=sub.parent_id');
		$res = $this->db->group('t1.id')->get($tables, 't1.*,count(sub.id) subNum', 't1.parent_id='. $pid );
		return $res;	
    }
    
    //post
    function index_post()
    {
        $this->title = \lang::g($this->title);
		$this->PU->check($this->auth_m);
        $id = intval($_GET['id']);
    	$this->t_var['data'] = $this->post($id);
        $this->setTemplate('post');
        $this->dispLayout = 'sub';
    }
    function post($id)
    {
        if($id)
    	 {
            $this->title = \lang::g('Edit') .\lang::g($this->title);
    	 	$data = $this->db->getOne($this->table, "*", 'id='.$id);
            $this->isExist($data);
    	 }
    	else{
            $this->title = \lang::g('Add') .\lang::g($this->title);
            $data = array('state'=>1);
            if (is_array($_GET['data']))
                $data = array_merge($data, $_GET['data']); 
    	 }
        return $data;
    }
    function index_save()
    {
        $this->PU->check($this->auth_m);
        $data = $_POST['data'];
        $id = intval($_POST['data_id']);
        $this->save($data, $id);
    }
    // save
    function save($data, $id)
    {
       if ($id)
		 {
			if ($this->is_p(intval($data['parent_id']), $id))
				d(\lang::g('class error'));
			$where = "id=$id";
		 }
		else{
			$where = '';
		 }
		
		isset($data['ordering']) && $data['ordering'] = intval($data['ordering']);
		isset($data['parent_id']) && $data['parent_id'] = intval($data['parent_id']);
        $b = $this->db->save($this->table, $data, $where);
		if ($b)
		 {
			if ($id)
				\log::write($this->table, 'edit', $id);
			else {
				$id = $this->db->insert_id();
				\log::write($this->table, 'add', $id);
			 }
			ajax_json(array('status'=>TRUE, 'id'=>$id));
		 }
        ajax_json(\lang::g('save fail')); 
    }

    
    // del
    function index_del()
    {
        $this->PU->check($this->auth_m);
        $ids = intval_ids($_GET['id']);
        
		if ($this->del($ids))
		 {
			\log::write($this->table, 'del', $ids);
			ajax_json(TRUE);	
		 }
		ajax_json(\lang::g( 'Over'));
    }
    function del($ids)
    {
        $this->db->save($this->table, array('parent_id'=>0), "parent_id IN($ids)");
        $this->db->del($this->table, "id IN($ids)");
        return $this->db->num_affected()>0;
    }
    
    // select win
    function index_select()
    {
        $this->title = \lang::g($this->title);
        $this->PU->check($this->auth_m);      
        $res = $this->getLi(intval($_GET['id']));

        if ($_GET['q']=='a')
            ajax_json(array('res'=>$res, 'status'=>TRUE));
        $this->setTemplate('select',  array('res'=>$res));
        $this->dispLayout = 'sub';
    }
}
?>
